package com.agritech.service.impl;

import com.agritech.entity.Knowledge;
import com.agritech.mapper.KnowledgeMapper;
import com.agritech.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 知识服务实现类
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {
    
    @Autowired
    private KnowledgeMapper knowledgeMapper;
    
    @Override
    public List<Knowledge> getKnowledgeByCategory(Long categoryId) {
        return knowledgeMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<Knowledge> list(String keywords) {
        return knowledgeMapper.list(keywords);
    }

    @Override
    public Knowledge getKnowledgeById(Long id) {
        return knowledgeMapper.selectById(id);
    }
    
    @Override
    public List<Knowledge> getLatestKnowledge(int limit) {
        return knowledgeMapper.selectLatest(limit);
    }
    
    @Override
    public boolean addKnowledge(Knowledge knowledge) {
        int result = knowledgeMapper.insert(knowledge);
        return result > 0;
    }
    
    @Override
    public boolean updateKnowledge(Knowledge knowledge) {
        int result = knowledgeMapper.update(knowledge);
        return result > 0;
    }
    
    @Override
    public boolean deleteKnowledge(Long id) {
        int result = knowledgeMapper.delete(id);
        return result > 0;
    }
    @Override
    public List<Knowledge> getKnowledgeList(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        return knowledgeMapper.getKnowledgeList(offset, pageSize);
    }

}    